package leetcode_core.test;

import java.lang.reflect.InvocationTargetException;

public class BookFactory {
    //工厂类,设置为构造方法为私有
    private BookFactory(){

    }
    public static IBook getInstance(String className){//传入子类的完整名称
        try{
            Object obj = Class.forName(className).getDeclaredConstructor().newInstance();
            //防止转换失败
            if(obj instanceof IBook){
                return (IBook) obj;
            }else{
                return null;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}
